package com.sh.healthtv.webconfig;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.webkit.JavascriptInterface;
import android.widget.Toast;

import com.sh.healthtv.app.AppManager;
import com.sh.healthtv.ui.PlayerActivity;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Created by zhouyucheng on 2017/4/1  14:14.
 */

public class KnowledgeJs implements ActivityFunc {
    private Activity mActivity;
    private String name;

    public String getName() {
        return name;
    }

    public KnowledgeJs(Activity mActivity) {
        this.mActivity = mActivity;
    }

    @JavascriptInterface
    public void finish() {
        mActivity.finish();
        AppManager.getAppManager().appExit(mActivity);
    }

    @JavascriptInterface
    @Override
    public void refreshTitle(String title) {
        this.name = title;
        System.out.println(title);
    }

    @JavascriptInterface
    @Override
    public void share(String a, String b, boolean c, String d) {
        System.out.println("微信分享回调" + a + ":a" + b + ":b" + c + "：c" + d + ":d");
        System.out.println("   调用了分享");
    }

    @JavascriptInterface
    @Override
    public void saveJkyxBitmap(byte[] temp, String bitName) {
        Bitmap bt = getBitmapFromByte(temp);
        File f = new File("/sdcard/Jkyx/" + bitName + ".jpg");
        FileOutputStream fOut = null;
        try {
            fOut = new FileOutputStream(f);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        bt.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
        try {
            fOut.flush();
        } catch (IOException e) {
            Toast.makeText(mActivity, "图形保存异常！", Toast.LENGTH_LONG).show();
            e.printStackTrace();
        }
        try {
            fOut.close();
        } catch (IOException e) {
            Toast.makeText(mActivity, "图形保存异常！", Toast.LENGTH_LONG).show();
            e.printStackTrace();
        }
        Toast.makeText(mActivity, "图形保存成功！", Toast.LENGTH_LONG).show();
    }

    @JavascriptInterface
    @Override
    public void onKeyClick(int onKeyCode) {

    }

    @JavascriptInterface
    @Override
    public void programPlay(String programId, String programName,String progress,String isConnect) {
        Log.i("1111", "programPlay:programPlay ");
        Intent in = new Intent();
        in.putExtra("programId", programId);
        in.putExtra("programName", programName);
        in.putExtra("progress",progress);
        in.putExtra("isConnect",isConnect);
        in.setClass(mActivity, PlayerActivity.class);
        mActivity.startActivity(in);
    }

    public Bitmap getBitmapFromByte(byte[] temp) {
        if (temp != null) {
            Bitmap bitmap = BitmapFactory.decodeByteArray(temp, 0, temp.length);
            return bitmap;
        } else {
            return null;
        }
    }
}
